home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48hor2 / cmat.src < prev    next >
Text File  |  1992-08-18  |  4KB  |  127 lines

  1. %%HP: T(3)A(R)F(.);
  2. @ CMAT: HP48 program to apply expressions to columns of a matrix.
  3. @       03/22/91 Version 1.0
  4. @       Wes Hubert <wes at kuhub.cc.ukans.edu>
  5. @
  6. @ CMAT provides operations on matrix columns.  To use it, place a matrix on
  7. @ level 2 of the stack, and an algebraic equation object or command list
  8. @ on level 1 of the stack.  (See external documentation for information
  9. @ about command lists.)  The equation should be of the form:
  10. @ 'Ci=expression', where "Ci" specifies the column where the
  11. @ result should be placed, and "expression" uses "C1, C2, ... Cn" to
  12. @ specify columns 1, 2, ... n of the matrix.  For example:
  13. @   [[1 2] [3 4] [5 6]]
  14. @   'C1=C2^2+C1'
  15. @ would return:
  16. @   [[5 2] [19 4] [41 6]]
  17. @
  18. @ If the output column is not in the original matrix, the matrix will be
  19. @ expanded to include enough new columns to store it, filling any new
  20. @ unreferenced columns with zero.  For example, if the equation for
  21. @ the input matrix above were 'C4=C2^2+C1', the result would be:
  22. @   [[1 2 0 5] [3 4 0 19] [5 6 0 41]]
  23. @
  24. @ CMAT creates a working directory, stores variables into it, and
  25. @ purges the directory when it finishes.  It includes rudimentary error
  26. @ trapping to purge the working directory even if it does not run to
  27. @ completion.
  28. @
  29. DIR
  30. CMAT                                @ Protect environment from data errors
  31.     \<< 'WRKTAB' CRDIR WRKTAB
  32.       IFERR CMAIN
  33.       THEN UPDIR 'WRKTAB' PGDIR ERRM DOERR
  34.       ELSE UPDIR 'WRKTAB' PGDIR
  35.       END
  36.     \>>
  37. CMAIN                               @ Main program for column processing
  38.    \<<
  39.      { } 'NAMES' STO
  40.      IF DUP TYPE 5 \=/
  41.      THEN 1 \->LIST
  42.      END
  43.      'FLIST' STO
  44.      1 FLIST SIZE
  45.      FOR elt FLIST elt GET
  46.        IF CPARSE
  47.        THEN CCOND
  48.            CRESULT
  49.            CADJUST
  50.            1 NR
  51.            FOR ir
  52.                ir 'CASEID' STO 1 NC
  53.                FOR ic ir ic 2 \->LIST
  54.                    GETI SWAP DROP
  55.                    NAMES ic GET STO
  56.                NEXT
  57.                IF CTST
  58.                THEN FORM EVAL \->NUM ir
  59.                     RESULT 2 \->LIST SWAP PUT
  60.                END
  61.            NEXT
  62.         END
  63.      NEXT
  64.    \>>
  65. CPARSE                              @ Scan item from command list.
  66.     \<<
  67.       IF DUP TYPE 5 \=/                      @ If not a list,
  68.       THEN 1                                 @   treat as algebraic equation
  69.       ELSE DUP 1 GET 1 1 SUB                 @ Check first char of keyword
  70.         CASE DUP "N" ==                      @ "NAMELIST"
  71.           THEN DROP 2 99 SUB CNL 0           @   Process in CNL, return 0
  72.           END DUP "I" ==                     @ "IF"
  73.           THEN DROP DUP 3 GET SWAP 2 GET 1   @   Return equation & condition
  74.           END "C" ==                         @ "COMPUTE"
  75.           THEN 2 GET 1                       @   Return equation only
  76.           END
  77.         END
  78.       END
  79.     \>>
  80. CNL                                 @ Process namelist {{position name}...}
  81.     \<<
  82.       IF DUP TYPE 5 ==
  83.       THEN 1 OVER SIZE
  84.         FOR i DUP i GET 1 GETI
  85.           IF DUP NAMES SIZE >
  86.           THEN NAMES SIZE 1 + OVER
  87.             FOR i NAMES "C" i +              @ Default name C+column#
  88.               # 5B15h SYSEVAL + 'NAMES' STO  @ String to variable name
  89.             NEXT
  90.           END
  91.           ROT ROT GET NAMES ROT
  92.           ROT PUT 'NAMES' STO
  93.         NEXT
  94.       END DROP
  95.     \>>
  96. CCOND                              @ Process conditional part, if present
  97.     \<<
  98.       IF DUP \->STR DUP "=" POS SWAP
  99.          "==" POS NOT AND
  100.       THEN 1                                 @ Default is TRUE (1)
  101.       END 'CTST' STO
  102.     \>>
  103. CRESULT                            @ Save result column # in RESULT
  104.     \<<
  105.       OBJ\-> DROP2 'FORM' STO
  106.       IF NAMES OVER POS DUP
  107.       THEN SWAP DROP
  108.       ELSE DROP \->STR 3 OVER
  109.       SIZE 1 - SUB OBJ\->
  110.       END 'RESULT' STO
  111.     \>>
  112. CADJUST                            @ Add columns to matrix and names
  113.     \<<
  114.       DUP SIZE DUP 1 GET 'NR' STO
  115.       2 GET 'NC' STO
  116.       IF NC RESULT <
  117.       THEN TRN RESULT NR 2
  118.            \->LIST RDM TRN
  119.       END
  120.       NAMES DUP SIZE 1 + NC
  121.       FOR i
  122.           "C" i + # 5B15h SYSEVAL +
  123.       NEXT
  124.      'NAMES' STO
  125.     \>>
  126. END
  127.